/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.huangjianbo.supos.openapi.core.http;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import io.gitee.huangjianbo.supos.openapi.core.enums.AuthorizationType;

/**
 * 请求头
 *
 * @author tomcat
 * @version 1.0
 */
public class HttpHeader {

    private final Map<String, String> HEADERS = new ConcurrentHashMap<>();

    private HttpHeader() {

    }

    public static HttpHeader of() {
        return new HttpHeader();
    }

    public static HttpHeader of(String key, String value) {
        HttpHeader instance = HttpHeader.of();
        return instance.add(key, value);
    }

    public static HttpHeader of(Map<String, String> headers) {
        HttpHeader instance = HttpHeader.of();
        return instance.add(headers);
    }

    public static HttpHeader of(HttpContentType contentType) {
        return of(HttpHeaders.CONTENT_TYPE, contentType.getMimeType());
    }

    public static HttpHeader ofContentTypeWithJson() {
        return of(HttpContentType.APPLICATION_JSON);
    }

    public Map<String, String> toMap() {
        return HEADERS;
    }

    public HttpHeader add(String key, String value) {
        HEADERS.put(key, value);
        return this;
    }

    public HttpHeader add(Map<String, String> headers) {
        HEADERS.putAll(headers);
        return this;
    }

    public HttpHeader add(HttpContentType contentType) {
        return add(HttpHeaders.CONTENT_TYPE, contentType.getMimeType());
    }

    public HttpHeader add(HttpLanguage acceptLanguage) {
        return add(HttpHeaders.ACCEPT_LANGUAGE, acceptLanguage.getAcceptLanguage());
    }

    public HttpHeader add(AuthorizationType authType, String auth) {
        return add(HttpHeaders.AUTHORIZATION, authType.name() + " " + auth);
    }

    public boolean containsKey(String key) {
        return this.HEADERS.containsKey(key);
    }

    public String get(String key) {
        return HEADERS.get(key);
    }

    public boolean isEmpty() {
        return this.HEADERS.isEmpty();
    }


}
